Passed
Push — master ( 7d3761...2cddad )
by Rafael S.
01:29
created

helpers.js ➔ turnToArray   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
/*
2
 * helpers: functions to work with bytes and byte arrays.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const endianness = require("endianness");
8
const bitDepths = require("../src/bit-depth.js");
9
10
/**
11
 * Padding for binary strings.
12
 * @param {!Array<string>} bytes The bytes as binary strings.
13
 * @param {number} base The base.
14
 * @param {number} index The byte to pad.
15
 */
16
function padding(bytes, base, index) {
17
    bytes[index] = bytePadding(bytes[index], base);
18
}
19
20
/**
21
 * Padding with 0s for byte strings.
22
 * @param {string} theByte The byte as a binary or hex string.
23
 * @param {number} base The base.
24
 * @returns {string} The padded byte.
25
 */
26
function bytePadding(theByte, base) {
27
    let offset = theByte.length + 1;
28
    if (base == 2) {
29
        offset = 8;
30
    } else if (base == 16) {
31
        offset = 2;
32
    }
33
    return lPadZeros(theByte, offset);
34
}
35
36
/**
37
 * Fix the size of nibbles.
38
 * @param {!Array<string>} nibbles The nibble as a binary or hex string.
39
 * @param {number} base The base.
40
 * @param {number} index The nibble offset.
41
 */
42
function paddingNibble(nibbles, base, index) {
43
    if (base == 2 && nibbles[index].length < 4) {
44
        nibbles[index] = 
45
            new Array((5 - nibbles[index].length)).join("0")  + nibbles[index];
46
    }
47
}   
48
49
/**
50
 * Fix the size of crumbs.
51
 * @param {!Array<string>} crumbs The nibble as a binary or hex string.
52
 * @param {number} base The base.
53
 * @param {number} index The nibble offset.
54
 */
55
function paddingCrumb(crumbs, base, index) {
56
    if ((base == 2 || base == 16) && crumbs[index].length < 2) {
57
        crumbs[index] = '0' + crumbs[index];
58
    }
59
}   
60
61
/**
62
 * Pad a string with zeros to the left.
63
 * TODO: This should support both arrays and strings.
64
 * @param {string} value The string (representing a binary or hex value).
65
 * @param {number} numZeros the max number of zeros.
66
 *      For 1 binary byte string it should be 8.
67
 */
68
function lPadZeros(value, numZeros) {
69
    while (value.length < numZeros) {
70
        value = '0' + value;
71
    }
72
    return value;
73
}
74
75
/**
76
 * Pad a array with zeros to the right.
77
 * @param {!Array<number>} byteArray The array.
78
 * @param {number} numZeros the max number of zeros.
79
 *      For 1 binary byte string it should be 8.
80
 *      TODO: better explanation of numZeros
81
 */
82
function fixByteArraySize(byteArray, numZeros) {
83
    let i = 0;
84
    let fix = byteArray.length % numZeros;
85
    if (fix) {
86
        fix = (fix - numZeros) * -1;
87
        while(i < fix) {
88
            byteArray.push(0);
89
            i++;
90
        }
91
    }
92
}
93
94
/**
95
 * Swap the endianness to big endian.
96
 * @param {!Array<number>} bytes The values.
97
 * @param {boolean} isBigEndian True if the bytes should be big endian.
98
 * @param {number} bitDepth The bitDepth of the data.
99
 */
100
function makeBigEndian(bytes, isBigEndian, bitDepth) {
101
    if (isBigEndian) {
102
        endianness(bytes, bitDepths.BitDepthOffsets[bitDepth]);
103
    }
104
}
105
106
/**
107
 * Turn bytes to base 2, 10 or 16.
108
 * @param {!Array<string>|!Array<number>} bytes The bytes.
109
 * @param {number} base The base.
110
 * @param {Function} padFunction The function to use for padding.
111
 */
112
function bytesToBase(bytes, base, padFunction=padding) {
113
    if (base != 10) {
114
        let i = 0;
115
        let len = bytes.length;
116
        while (i < len) {
117
            bytes[i] = bytes[i].toString(base);
118
            padFunction(bytes, base, i);
119
            i++;
120
        }
121
    }
122
}
123
124
/**
125
 * Turn the output to the correct base.
126
 * @param {!Array<number>} bytes The bytes.
127
 * @param {number} bitDepth The bit depth of the data.
128
 * @param {number} base The desired base for the output data.
129
 */
130
function outputToBase(bytes, bitDepth, base) {
131
    if (bitDepth == 4) {
132
        bytesToBase(bytes, base, paddingNibble);
133
    } else if (bitDepth == 2) {
134
        bytesToBase(bytes, base, paddingCrumb);
135
    } else if(bitDepth == 1) {
136
        bytesToBase(bytes, base, function(){});
137
    }else {
138
        bytesToBase(bytes, base);
139
    }
140
}
141
142
/**
143
 * Make a single value an array in case it is not.
144
 * If the value is a string it stays a string.
145
 * @param {!Array<number>|number|string} values The value or values.
146
 * @return {!Array<number>|string}
147
 */
148
function turnToArray(values) {
149
    if (!Array.isArray(values) && typeof values != "string") {
150
        values = [values];
151
    }
152
    return values;
153
}
154
155
/**
156
 * Turn a unsigned number to a signed number.
157
 * @param {number} num The number.
158
 * @param {number} maxValue The max range for the number bit depth.
159
 */
160
function signed(num, maxValue) {
161
    if (num > parseInt(maxValue / 2, 10) - 1) {
162
        num -= maxValue;
163
    }
164
    return num;
165
}
166
167
/**
168
 * Turn bytes to base 10.
169
 * @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings.
170
 * @param {number} base The base.
171
 */
172
function bytesToInt(bytes, base) {
173
    if (base != 10) {
174
        let i = 0;
175
        let len = bytes.length;
176
        while(i < len) {
177
            bytes[i] = parseInt(bytes[i], base);
178
            i++;
179
        }
180
    }
181
}
182
183
function fixFloat16Endianness(bytes, options) {
184
    if (options.float && options.bits == 16) {
185
        endianness(bytes, 2);
186
    }
187
}
188
189
module.exports.makeBigEndian = makeBigEndian;
190
module.exports.bytesToBase = bytesToBase;
191
module.exports.outputToBase = outputToBase;
192
module.exports.turnToArray = turnToArray;
193
module.exports.signed = signed;
194
module.exports.bytesToInt = bytesToInt;
195
module.exports.fixByteArraySize = fixByteArraySize;
196
module.exports.padding = padding;
197
module.exports.paddingNibble = paddingNibble;
198
module.exports.paddingCrumb = paddingCrumb;
199
module.exports.bytePadding = bytePadding;
200
module.exports.lPadZeros = lPadZeros;
201
module.exports.fixFloat16Endianness = fixFloat16Endianness;
202